/**
 * Created by sun on 2016/8/4.
 */
define(function(require) {
	var app = require('/../js/app');
	require('ui-table');

	var toastr =require('toastr');

	app.useModule("ui.table");



	var datepicker = require('datepicker');
	app.controller('guarantee_itemCtrl', ['$scope', '$http','$filter',
		function($scope, $http,$filter) {
			$scope.orderParams = {
				url: "/admin/invest/repayListByGuarantee"
			};
			datepicker($scope);
			$scope.ctrlScope=$scope;

			//创建
			$scope.addOrderModal = function() {
				$scope.orderModal = !$scope.orderModal;
			};
			$scope.getShow=function(parm){
				$http({
					method: 'POST',
					url: "/admin/invest/statRepayByGuarantee",
					data:parm,
				}).success(function(data) {
					console.info(data);
					$scope.show=data;
				}).error(function(data) {});
			}
			$scope.getShow();

			//全局查询
			$scope.search = function() {
				$scope.ctrlScope.itemsearch.repayStartTime=$scope.ctrlScope.itemsearch.repayStartTimex;
				$scope.ctrlScope.itemsearch.repayEndTime=$scope.ctrlScope.itemsearch.repayEndTimex;
				$scope.ctrlScope.itemsearch.actualStartTime=$scope.ctrlScope.itemsearch.actualStartTimex;
				$scope.ctrlScope.itemsearch.actualEndTime=$scope.ctrlScope.itemsearch.actualEndTimex;
				$scope.ctrlScope.itemsearch.repayStartTime=$filter('date')($scope.ctrlScope.itemsearch.repayStartTime,'yyyy-MM-dd');
				$scope.ctrlScope.itemsearch.repayEndTime=$filter('date')($scope.ctrlScope.itemsearch.repayEndTime,'yyyy-MM-dd');
				$scope.ctrlScope.itemsearch.actualStartTime=$filter('date')($scope.ctrlScope.itemsearch.actualStartTime,'yyyy-MM-dd');
				$scope.ctrlScope.itemsearch.actualEndTime=$filter('date')($scope.ctrlScope.itemsearch.actualEndTime,'yyyy-MM-dd');
				//表格查询参数设置
				$scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
				//表格刷新
				$scope.orderParams.reload();
				var parm=$scope.ctrlScope.itemsearch;
				$scope.getShow(parm);
			};
			//全局查询重置
			$scope.reset=function(){
				$scope.orderParams.ajaxParam={};
				$scope.ctrlScope.itemsearch={};
				$scope.orderParams.reload();
				$scope.getShow();
			};

//还款
			$scope.modifyModal = function(itemId, repayCurrentPeriod,repayType) {
				var parm = {
					"itemId": itemId,
					"currentPeriod" : repayCurrentPeriod,
					"repayType" : repayType
				}
				if (confirm("确认还款?")) {
					$http({
						method: 'POST',
						url: "/admin/item/doRepay",
						data: parm,
					}).success(function(data) {
						console.log(data)
						if(data.result=='fail'){
							if(data.messageText){
								toastr.error(data.messageText)
							}else{
								toastr.error('还款失败')
							}

						}else{
							toastr.success('还款成功');
							$scope.orderParams.reload();
							$scope.getShow();
						}

					}).error(function(data) {});
				} else {
					return
				}
			};
		}
	]);


});